package com.andy.eqm.server.dto;

import java.util.Map;
import java.util.Map.Entry;

import com.andy.eqm.server.entity.UsrGameInfo;
import com.andy.eqm.server.game.GameUser;

import io.netty.buffer.ByteBuf;

/**
 * 用户登录
 * @author cuiwm
 */
public class UserMessage extends BaseMessage{
	
    private GameUser user;
    
    public UserMessage(GameUser user) {
    	this.user = user;
	}

	@Override
	public void writeBody(ByteBuf buf) {
		if(user == null){
			buf.writeInt(0);
		}else{
			UsrGameInfo gameInfo = user.getGameInfo();
			buf.writeInt(user.getId().intValue());
			writeString(buf,user.getUserName());
			buf.writeLong(gameInfo.getCoin());
			writeString(buf,user.getNickName());
			buf.writeInt(user.getUser().getIcon());
			
			Map<Integer, Long> items = gameInfo.items();
			buf.writeInt(items.size());
			for(Entry<Integer, Long> entry:items.entrySet()){
				buf.writeInt(entry.getKey());
				buf.writeInt(entry.getValue().intValue());
			}
		}
	}


}